package com.example.mq.polling;

import com.example.mq.utils.RabbitMqUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

//启动两个工作线程
public class ReceiveMq {

    private static final String QUEUE_NAME = "mq-hello";

    public static void main(String[] args) throws Exception {

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    Channel channel = RabbitMqUtils.getChannel();
                    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
                        String receivedMessage = new String(delivery.getBody());
                        System.out.println(Thread.currentThread().getName() + "接收到消息:" + receivedMessage);
                    };
                    CancelCallback cancelCallback = (consumerTag) -> {
                        System.out.println(Thread.currentThread().getName() + "消费者取消消费接口回调逻辑");
                    };
                    System.out.println(Thread.currentThread().getName() + "消费者启动等待消费......");
                    channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(runnable).start();
        new Thread(runnable).start();

    }

}
